package org.example.yupao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.example.yupao.common.BaseResponse;
import org.example.yupao.common.ErrorCode;
import org.example.yupao.common.ResultUtils;
import org.example.yupao.domain.User;
import org.example.yupao.domain.request.UserLoginRequest;
import org.example.yupao.domain.request.UserRegisterRequest;
import org.example.yupao.exception.BusineseException;
import org.example.yupao.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static org.example.yupao.constant.UserConstant.ADMIN_ROLE;
import static org.example.yupao.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusineseException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassWord = userRegisterRequest.getUserPassWord();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAnyBlank(userAccount,userPassWord,checkPassword,planetCode)) {
            throw new BusineseException(ErrorCode.PARAM_ERROR);
        }
        long result = userService.userRegister(userAccount, userPassWord, checkPassword, planetCode);
        return ResultUtils.success(result);
    }

    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null || request == null) {
            throw new BusineseException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassWord = userLoginRequest.getUserPassWord();
        if (StringUtils.isAnyBlank(userAccount,userPassWord)) {
            return null;
        }
        User user = userService.userLogin(userAccount, userPassWord, request);
        System.out.println(user);
        return user;
    }

    @PostMapping("/logout")
    public BaseResponse<Integer> userLoout(HttpServletRequest request) {
        if (request == null) {
            throw new BusineseException(ErrorCode.PARAM_ERROR);
        }
        int userLogout = userService.userLogout(request);
        return ResultUtils.success(userLogout);
    }

    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User current = (User)userObj;
        if (current == null) {
            throw new BusineseException(ErrorCode.PARAM_ERROR);
        }
        long userId = current.getId();
        User user = userService.getById(userId);
        User safetyUser = userService.getsaftyUser(user);
        return ResultUtils.success(safetyUser);
    }

    @GetMapping("/search")
    public BaseResponse<List<User>> searchUsers(String username,HttpServletRequest request) {
        if (!isAdmin(request)) {
            throw new BusineseException(ErrorCode.NOT_LOGIN);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<User> userList = userService.list(queryWrapper);
        List<User> list = userList.stream().map(user -> userService.getsaftyUser(user)).collect(Collectors.toList());
        return ResultUtils.success(list);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody long id,HttpServletRequest request) {
        if (id <= 0 || request == null) {
            throw new BusineseException(ErrorCode.NOT_AUTH,"缺少权限");
        }
        Boolean b = userService.removeById(id);
        return ResultUtils.success(b);
    }

    private boolean isAdmin(HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) attribute;
        if (user != null && user.getUserRole() == ADMIN_ROLE) {
            return true;
        }
        return false;
    }

}
